package com.ruoyi.safe.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.safe.domain.BizSafeAttach;
import com.ruoyi.safe.domain.BizSafeFine;
import com.ruoyi.safe.domain.CommonCooperator;
import com.ruoyi.safe.domain.CommonProject;
import com.ruoyi.safe.mapper.*;
import com.ruoyi.safe.service.IBizSafeFineService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 安全罚款台账Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-11
 */
@Service
public class BizSafeFineServiceImpl implements IBizSafeFineService {
    @Value("${minio.bucketName}")
    private String bucketName;
    @Autowired
    private BizSafeFineMapper bizSafeFineMapper;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private CommonCooperatorMapper commonCooperatorMapper;
    @Autowired
    private CommonProjectMapper commonProjectMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private BizSafeAttachServiceImpl bizSafeAttachService;
    /**
     * 查询安全罚款台账
     *
     * @param id 安全罚款台账主键
     * @return 安全罚款台账
     */
    @Override
    public BizSafeFine selectBizSafeFineById(Long id) {
        BizSafeFine bizSafeFine = bizSafeFineMapper.selectBizSafeFineById(id);
        bizSafeFine.setBucket(bucketName);
        SysUser sysUser = sysUserMapper.selectUserByUserName(bizSafeFine.getCreateBy());
        bizSafeFine.setCreateByNickName(sysUser.getNickName());

        if(StringUtils.isNotNull(bizSafeFine.getFineAttachment()) && !"".equals(bizSafeFine.getFineAttachment())){
            String[] arr = bizSafeFine.getFineAttachment().split(",");
            Long[] photoIds = new Long[arr.length];
            for(int i=0;i<arr.length;i++){
                photoIds[i] = Long.parseLong(arr[i]);
            }
            List<BizSafeAttach> bizSafeAttaches = bizSafeAttachService.selectBizSafeAttachByBizIds(photoIds);
            bizSafeFine.setFineAttachments(bizSafeAttaches);
        }

        return bizSafeFine;
    }

    /**
     * 查询安全罚款台账列表
     *
     * @param bizSafeFine 安全罚款台账
     * @return 安全罚款台账
     */
    @Override
    public List<BizSafeFine> selectBizSafeFineList(BizSafeFine bizSafeFine) {
        if(StringUtils.isNotNull(bizSafeFine.getFineSource()) && !"".equals(bizSafeFine.getFineSource())){
            String dictLabel = DictUtils.getDictLabel("biz_fine_source", bizSafeFine.getFineSource());
            bizSafeFine.setFineSource(dictLabel);
        }
        List<BizSafeFine> list = bizSafeFineMapper.selectBizSafeFineList(bizSafeFine);
        list.forEach(item -> {
            item.setBucket(bucketName);
            SysUser sysUser = sysUserMapper.selectUserByUserName(item.getCreateBy());
            item.setCreateByNickName(sysUser.getNickName());
        });
        return list;
    }

    /**
     * 新增安全罚款台账(对分包罚款)
     *
     * @param bizSafeFine 安全罚款台账
     * @return 结果
     */
    @Override
    public int insertBizSafeFine(BizSafeFine bizSafeFine, SysUser sysUser) {
        bizSafeFine.setCreateTime(DateUtils.getNowDate());
        bizSafeFine.setCreateBy(sysUser.getUserName());
//        bizSafeFine.setFineButton(bizSafeFine.getFineButton());//新增对分包罚款时设置为1
        if (bizSafeFine.getFineUser() != null && !bizSafeFine.getFineUser().isEmpty())
        {
            //罚款单位
            String fineUnit = bizSafeFineMapper.selectDeptName(sysUser.getDeptId());
            bizSafeFine.setFineUnit(fineUnit);

            List<Map<String, Object>> list = commonCooperatorMapper.selectSimpleList(new CommonCooperator());
            list.forEach(item -> {
                String id = item.get("id").toString();
                if (bizSafeFine.getFineUser().equals(id))
                {
                    String enterpriseName = item.get("enterprise_name").toString();
                    bizSafeFine.setFineUser(enterpriseName);
                }
            });
        }
        else
        {
            //罚款单位
            String fineUnit = bizSafeFineMapper.selectDeptName(sysUser.getDeptId());
            bizSafeFine.setFineUnit(fineUnit);

            List<Map<String, Object>> list = commonProjectMapper.selectSimpleList(new CommonProject());
            list.forEach(item -> {
                String id = item.get("id").toString();
                if (bizSafeFine.getProjectId().toString().equals(id))
                {
                    String name = item.get("name").toString();
                    bizSafeFine.setFineUser(name);
                }
            });
        }
        String dictLabel = DictUtils.getDictLabel("biz_fine_source", bizSafeFine.getFineSource());
        bizSafeFine.setFineSource(dictLabel);
        //判断编号是否存在
        int count = bizSafeFineMapper.selectCount(bizSafeFine.getSafeNumber());
        if (count > 0) {
            throw new SecurityException("该编号已存在，请重新输入信息");
        }
        return bizSafeFineMapper.insertBizSafeFine(bizSafeFine);
    }

    /**
     * 添加对项目罚款(对项目罚款)
     *
     * @param bizSafeFine
     * @return
     */
    @Override
    public int insertProgram(BizSafeFine bizSafeFine, SysUser sysUser) {
        bizSafeFine.setCreateTime(DateUtils.getNowDate());
        bizSafeFine.setCreateBy(sysUser.getUserName());
        bizSafeFine.setFineButton(bizSafeFine.getFineButton());//新增对项目罚款设置为2
        if (bizSafeFine.getProjectId() == null)
        {
            bizSafeFine.setProjectId(Long.parseLong(bizSafeFine.getFineUser()));
        }
        //罚款单位
        String fineUnit = bizSafeFineMapper.selectDeptName(sysUser.getDeptId());
        bizSafeFine.setFineUnit(fineUnit);

        List<Map<String, Object>> list = commonProjectMapper.selectSimpleList(new CommonProject());
        list.forEach(item -> {
            String id = item.get("id").toString();
            if (bizSafeFine.getFineUser().equals(id))
            {
                String name = item.get("name").toString();
                bizSafeFine.setFineUser(name);
            }
        });
        String dictLabel = DictUtils.getDictLabel("biz_fine_source", bizSafeFine.getFineSource());
        bizSafeFine.setFineSource(dictLabel);
        //公司级用户可以对所属公司下的项目进行罚款
        SysDept sysDept = deptMapper.selectDeptById(SecurityUtils.getDeptId());
        if (sysDept.getDeptName().contains("公司")) {
            int count = bizSafeFineMapper.selectCount(bizSafeFine.getSafeNumber());
            if (count > 0) {
                throw new SecurityException("该编号已存在，请重新输入信息");
            }
            return bizSafeFineMapper.insertProgram(bizSafeFine);
        } else {
            throw new SecurityException("只有公司级用户才可以新增");
        }

    }

    /**
     * 修改安全罚款台账
     *
     * @param bizSafeFine 安全罚款台账
     * @return 结果
     */
    @Override
    public int updateBizSafeFine(BizSafeFine bizSafeFine, SysUser sysUser) {
        bizSafeFine.setUpdateTime(DateUtils.getNowDate());
        bizSafeFine.setUpdateBy(sysUser.getUserName());
        //罚款单位
        String fineUnit = bizSafeFineMapper.selectDeptName(sysUser.getDeptId());
        bizSafeFine.setFineUnit(fineUnit);
        return bizSafeFineMapper.updateBizSafeFine(bizSafeFine);
    }

    /**
     * 批量删除安全罚款台账
     *
     * @param ids 需要删除的安全罚款台账主键
     * @return 结果
     */
    @Override
    public int deleteBizSafeFineByIds(Long[] ids) {
        return bizSafeFineMapper.deleteBizSafeFineByIds(ids);
    }

    /**
     * 删除安全罚款台账信息
     *
     * @param id 安全罚款台账主键
     * @return 结果
     */
    @Override
    public int deleteBizSafeFineById(Long id) {
        return bizSafeFineMapper.deleteBizSafeFineById(id);
    }

    @Override
    public List<BizSafeFine> getSelectList() {
        return bizSafeFineMapper.selectList();
    }

    @Override
    public Long getSelectMaxId() {
        return bizSafeFineMapper.selectMaxId();
    }

    @Override
    public List<BizSafeFine> selectProgramList(BizSafeFine bizSafeFine) {
        if(StringUtils.isNotNull(bizSafeFine.getFineSource()) && !"".equals(bizSafeFine.getFineSource())){
            String dictLabel = DictUtils.getDictLabel("biz_fine_source", bizSafeFine.getFineSource());
            bizSafeFine.setFineSource(dictLabel);
        }
        List<BizSafeFine> list = bizSafeFineMapper.selectBizSafeFineList(bizSafeFine);
//        List<BizSafeFine> list = bizSafeFineMapper.selectProgramList(bizSafeFine);
        list.forEach(item -> {
            item.setBucket(bucketName);
            SysUser sysUser = sysUserMapper.selectUserByUserName(item.getCreateBy());
            item.setCreateByNickName(sysUser.getNickName());
        });
        return list;
    }
}
